<?php

namespace app\model;

use think\Model;
use think\model\relation\HasOne;

class StationOilNumber extends Model
{
    public function saveData(): array
    {
        $param = request()->post();
        $this->exists(!empty($param['id']))->save($param);
        return $this->toArray();
    }

    public function category(): HasOne
    {
        return $this->hasOne(StationOilCategory::class, "id", "type");
    }

    public function pageData(): array
    {
        $param = request()->param();
        $data = $this->with(['category'])->when(!empty($param['name']), function ($query) use ($param) {
            $query->whereLike('name', "%{$param['name']}%");
        })->when(!empty($param['type']), function ($query) use ($param) {
            $query->where('type', $param['type']);
        })->when(!empty($param['state']), function ($query) use ($param) {
            $query->where('state', $param['state']);
        })->order('create_time', 'desc')->paginate(['list_rows' => 30, 'page' => $param['page'] ?? 1])->toArray();
        return pageData($data);
    }
}
